﻿using System;

namespace MultiConvert
{
    internal abstract class SEDocumentConverter : MultiConvertDocumentConverter
    {
        protected SEApplicationWrapper Application;        
        internal SolidEdgeFramework.HighlightSets HighlightSets { get; set; }        

        protected String SaveAs(SolidEdgeFramework.SolidEdgeDocument Document, String toFileName, String ConvertMethod)
        {
        	try {
            
            	Document.SaveAs( toFileName );
            	
            	String[] Target = ConvertMethod.Split(new[] {'2'} );
            	switch (Target[1])
	            {
	                case "x_t":
            			return Properties.Resources.MsgConvX_TSuccess;
	            	case "igs":
            			return Properties.Resources.MsgConvIGSSuccess;
	                case "jt":
            			return Properties.Resources.MsgConvJTSuccess;
	                case "stp":
            			return Properties.Resources.MsgConvSTPSuccess;                    
            		default:
            			return Properties.Resources.MsgConvSuccess;            			
            	}
            
            } catch {
            	
            	return Properties.Resources.MsgConvFailed;
            }     	
        	
        }

        protected String SaveAsImage(SolidEdgeFramework.SolidEdgeDocument Document, String toFileName, String ConvertMethod)
        {
            SolidEdgeFramework.Windows Windows = Document.Windows;
            SolidEdgeFramework.Window Window = (SolidEdgeFramework.Window)Windows.Item(1);
            SolidEdgeFramework.View View = Window.View;
            SolidEdgePart.RefPlanes RefPlanes = null;
            SolidEdgePart.RefAxes RefAxes = null;
            SolidEdgePart.Models Models = null;
            SolidEdgePart.WeldmentModels WModels = null;
                        

            SolidEdgePart.PartDocument Part = Document as SolidEdgePart.PartDocument;
            if (Part != null)
            {
                RefPlanes = Part.RefPlanes;
                RefAxes = Part.RefAxes;
                Models = Part.Models;
                
                Part.CoordinateSystems.Visible = false;                
            }

            SolidEdgePart.SheetMetalDocument Sheet = Document as SolidEdgePart.SheetMetalDocument;
            if (Sheet != null)
            {
                RefPlanes = Sheet.RefPlanes;
                RefAxes = Sheet.RefAxes;
                Models = Sheet.Models;
                Sheet.CoordinateSystems.Visible = false;
            }

            SolidEdgePart.WeldmentDocument Weldment = Document as SolidEdgePart.WeldmentDocument;
            if (Weldment != null)
            {
                RefPlanes = Weldment.RefPlanes;
                RefAxes = Weldment.RefAxes;
                WModels = Weldment.WeldmentModels;
                Weldment.CoordinateSystems.Visible = false;

            }

            if (RefPlanes != null)            
            {
                try
                {
                    foreach (SolidEdgePart.RefPlane Plane in RefPlanes)
                    {
                        try
                        {
                            Plane.Visible = false;
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                catch (Exception ex)
                {

                }
            }

            if (RefAxes != null)
            {
                try
                {
                    foreach (SolidEdgePart.RefAxis Axis in RefAxes)
                    {
                        try
                        {
                            Axis.Visible = false;
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }

            if (Models != null)
            {
                try
                {
                    foreach (SolidEdgePart.Model Model in Models)
                    {
                        try
                        {
                            Model.DisplayCenterOfMass = false;
                            Model.DisplayCenterOfVolume = false;
                            Model.DisplayPrincipalAxes = false;
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                catch (Exception es)
                {   
                }                
            }

            if (WModels != null)
            {
                try
                {
                    foreach (SolidEdgePart.WeldmentModel Model in WModels)
                    {
                        try
                        {
                            Model.DisplayCenterOfMass = false;
                            Model.DisplayCenterOfVolume = false;
                            Model.DisplayPrincipalAxes = false;
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                catch (Exception ex)
                {   
                }                
            }

            SolidEdgeAssembly.AssemblyDocument Assembly = Document as SolidEdgeAssembly.AssemblyDocument;
            if (Assembly != null)
            {
                try
                {
                    Assembly.PhysicalProperties.DisplayCenterOfMass = false;
                    Assembly.PhysicalProperties.DisplayCenterOfVolume = false;
                    Assembly.PhysicalProperties.DisplayPrincipalAxes = false;
                    Assembly.AsmRefPlanes.Visible = false;
                    Assembly.CoordinateSystems.Visible = false;
                }
                catch (Exception ex)
                {   
                }
                
            }

            //Double ex;
            //Double ey;
            //Double ez;
            //Double tx;
            //Double ty;
            //Double tz;
            //Double ux;
            //Double uy;
            //Double uz;
            //Boolean p;
            //Double sc;

            //View.GetCamera(out ex, out ey, out ez, out tx, out ty, out tz, out ux, out uy, out uz, out p, out sc);
            
            //ex = ex * sc;
            //ey = ey * sc;
            //ez = ez * sc;
            //tx = tx * sc;
            //ty = ty * sc;
            //tz = tz * sc;
            //ux = ux * sc;
            //uy = uy * sc;
            //uz = uz * sc;

            //Array Matrix = new Double[16];
            //View.ViewToGLProjection(ref Matrix);

            //View.SetCamera(ex, ey, ez, tx, ty, tz, ux, uy, uz, p, sc);   

            // Set Window to 400x400
            Window.WindowState = 0;
            Window.Height = 400;
            Window.Width = 400;
            
            View.Fit();
            View.SaveAsImage(toFileName, 400, 400);
            String Style = View.Style;
            return "Conversion to Image was sucessfull";
        }
    }

    interface ISEGeometry : IComparable
    {
        Object getGeometry();        
    }
}
